package com.lzj.admin.controller;

import com.lzj.admin.model.RespBean;
import com.lzj.admin.pojo.SaleListPayment;
import com.lzj.admin.query.SaleListPaymentQuery;
import com.lzj.admin.service.ISaleListPaymentService;
import com.lzj.admin.service.IUserService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;
import java.security.Principal;

/**
 * <p>
 * 销售单支付明细表 前端控制器
 * </p>
 *
 * @author luoyoumou
 * @since 2023-02-08
 */
@Controller
@RequestMapping("/saleListPayment")
public class SaleListPaymentController {

    @Resource
    private ISaleListPaymentService saleListPaymentService;

    @Resource
    private IUserService userService;

    /**
     * 销售支付明细管理主页
     * @return
     */
    @RequestMapping("index")
    public String index(){
        return "saleListPayment/saleListPayment";
    }

    /**
     * 销售支付明细列表查询接口
     * @param saleListPaymentQuery
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> saleListPaymentList(SaleListPaymentQuery saleListPaymentQuery){

        return saleListPaymentService.saleListPaymentList(saleListPaymentQuery);
    }

    /**
     * 添加|更新 销售支付明细页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("addOrUpdateSaleListPaymentPage")
    public String addOrUpdateSaleListPaymentPage(Integer id, Integer saleListId, String saleNumber, BigDecimal amountNotPaid, Integer payType, Model model){
        if(null != id){
            model.addAttribute("saleListPayment",saleListPaymentService.getById(id));
            model.addAttribute("amountNotPaid",amountNotPaid);
            return "saleListPayment/update";
        } else {
            model.addAttribute("saleListId",saleListId);
            model.addAttribute("saleNumber",saleNumber);
            model.addAttribute("amountNotPaid",amountNotPaid);
            model.addAttribute("payType",payType);
            return "saleListPayment/add";
        }
    }

    /**
     * 销售单支付信息添加
     * @param saleListPayment
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public RespBean saveSaleListPayment(SaleListPayment saleListPayment,Principal principal){
        Integer userId = userService.findUserByUserName(principal.getName()).getId();
        saleListPayment.setUserId(userId);
        saleListPayment.setUpdateUserId(userId);
        saleListPaymentService.saveSaleListPayment(saleListPayment);
        return RespBean.success("销售单支付记录添加成功！");
    }

    /**
     * 销售单支付信息更新
     * @param saleListPayment
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public RespBean updateSaleListPayment(SaleListPayment saleListPayment, Principal principal){
        Integer userId = userService.findUserByUserName(principal.getName()).getId();
        saleListPayment.setUpdateUserId(userId);
        saleListPaymentService.updateSaleListPayment(saleListPayment);
        return RespBean.success("销售单支付记录更新成功！");
    }

    @RequestMapping("delete")
    @ResponseBody
    public RespBean delete(Integer id, Principal principal) {
        Integer userId = userService.findUserByUserName(principal.getName()).getId();
        saleListPaymentService.deleteSaleListPayment(id,userId);
        return RespBean.success("销售单支付记录删除成功");
    }

}
